package com.crwlr.sandd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum EStrategy {
	SPORT_SCHOOL_STRTG() {
		private static final String DIV_CAT = "<div class=\"categories\">";
		private static final String DIV_SCHLS = "class=\"title\"";
		private static final String HTML_A_TAG_PATTERN = "(?i)<a([^>]+)>(.+?)</a>";
		private static final String HTML_A_HREF_TAG_PATTERN = "\\s*(?i)href\\s*=\\s*(\"([^\"]*\")|'[^']*'|([^'\">\\s]+))";

		private Pattern patternTag, patternLink;
		private Matcher matcherTag, matcherLink;

		{
			initializeRegEx();
		};

		// http://www.mkyong.com/regular-expressions/how-to-extract-html-links-with-regular-expression/
		// http://www.mkyong.com/regular-expressions/10-java-regular-expression-examples-you-should-know/

		@Override
		public void crawl(String urlStr) {
//			URL my_url;
			Map<String, String> secondLevelLinks, firstLevelLinks;
			firstLevelLinks = getLinksbyFilter(urlStr,
					DIV_CAT);
			for (String key : firstLevelLinks.keySet()) {
				secondLevelLinks = getLinksbyFilter(key, DIV_SCHLS);
			}
			// try {
			// BufferedReader br = downloadPage(urlStr);
			// String strTemp = "";
			// while (null != (strTemp = br.readLine())) {
			// if (strTemp.contains(DIV_CAT)) {
			// System.out.println("find!");
			// // System.err.println(strTemp);
			// System.out.println("regex work begin!");
			// matcherTag = patternTag.matcher(strTemp);
			// while (matcherTag.find()) {
			//
			// String href = matcherTag.group(1); // href
			// String linkText = matcherTag.group(2); // link text
			//
			// matcherLink = patternLink.matcher(href);
			// while (matcherLink.find()) {
			// String link = matcherLink.group(1); // link
			// System.err.println("link = " + link
			// + " text = " + linkText);
			// linksPoolSchools.put(link, linkText);
			// crawlSection(link);
			// break;
			// }
			//
			// }
			// }
			// }
			// } catch (MalformedURLException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// } catch (IOException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }

		}

		private Map<String, String> getLinksbyFilter(String url, String filter) {
			Map<String, String> linksMap = new HashMap<String, String>();
			BufferedReader br;
			try {
				br = downloadPage(url);
				String strTemp = "";
				while (null != (strTemp = br.readLine())) {
					if (strTemp.contains(filter)) {
						while (matcherTag.find()) {

							String href = matcherTag.group(1); // href
							String linkText = matcherTag.group(2); // link text

							matcherLink = patternLink.matcher(href);
							while (matcherLink.find()) {
								String link = matcherLink.group(1); // link
								System.err.println("DIV_SCHLS link = " + link
										+ " text = " + linkText);
								linksMap.put(link, linkText);
							}
						}
					}
				}

			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return linksMap;

		}

		private BufferedReader downloadPage(String urlStr)
				throws MalformedURLException, IOException {
			URL my_url;
			my_url = new URL(urlStr);
			BufferedReader br = new BufferedReader(new InputStreamReader(
					my_url.openStream()));
			return br;
		}

		// private void crawlSection(String url){
		// BufferedReader br;
		// try {
		// br = downloadPage(url);
		// String strTemp = "";
		// while (null != (strTemp = br.readLine())) {
		// if(strTemp.contains(DIV_SCHLS)){
		// while (matcherTag.find()) {
		//
		// String href = matcherTag.group(1); // href
		// String linkText = matcherTag.group(2); // link text
		//
		// matcherLink = patternLink.matcher(href);
		// while (matcherLink.find()) {
		// String link = matcherLink.group(1); // link
		// System.err.println("DIV_SCHLS link = " + link
		// + " text = " + linkText);
		// }
		// }
		// }
		// }
		// } catch (MalformedURLException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// }

		private void initializeRegEx() {
			patternTag = Pattern.compile(HTML_A_TAG_PATTERN);
			patternLink = Pattern.compile(HTML_A_HREF_TAG_PATTERN);
		}

	};

	private static Map<String, String> linksPoolSchools = new HashMap<String, String>();

	// link sport type, map of links correspond existed school
	private Map<String, Map<String, SportSchool>> linksPool = new HashMap<String, Map<String, SportSchool>>();

	public abstract void crawl(String urlStr);

}
